package experimentThird;

import java.util.Scanner;

public class StorageManagement {

    public static void main(String[] args) {
        MemoryManager memoryManager = new MemoryManager(600); // 初始空闲区为600KB
        System.out.println("初始空闲区为"+memoryManager.getHead().size+"KB");
        // 请求序列
        System.out.println("首次适应算法测试：");
        runTest(memoryManager, "firstFit");

        memoryManager = new MemoryManager(600); // 重新初始化空闲区为600KB
        System.out.println("\n循环首次适应算法测试：");
        runTest(memoryManager, "nextFit");

        memoryManager = new MemoryManager(600); // 重新初始化空闲区为600KB
        System.out.println("\n最佳适应算法测试：");
        runTest(memoryManager, "bestFit");
    }

    private static void runTest(MemoryManager memoryManager, String algorithm) {
        // 作业1申请130KB
        allocate(memoryManager, algorithm, 130);
        // 作业2申请60KB
        allocate(memoryManager, algorithm, 60);
        // 作业3申请100KB
        allocate(memoryManager, algorithm, 100);
        // 作业2释放60KB
        memoryManager.freePartition(130);
        memoryManager.displayFreePartitions();
        // 作业4申请200KB
        allocate(memoryManager, algorithm, 200);
        // 作业3释放100KB
        memoryManager.freePartition(190);
        memoryManager.displayFreePartitions();
        // 作业1释放130KB
        memoryManager.freePartition(0);
        memoryManager.displayFreePartitions();
        // 作业5申请140KB
        allocate(memoryManager, algorithm, 140);
        // 作业6申请60KB
        allocate(memoryManager, algorithm, 60);
        // 作业7申请50KB
        allocate(memoryManager, algorithm, 50);
    }

    private static void allocate(MemoryManager memoryManager, String algorithm, int size) {
        switch (algorithm) {
            case "firstFit":
                memoryManager.firstFit(size);
                break;
            case "nextFit":
                memoryManager.nextFit(size);
                break;
            case "bestFit":
                memoryManager.bestFit(size);
                break;
            default:
                System.out.println("无效的算法选择");
        }
        memoryManager.displayFreePartitions();
    }


}
